/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.managedbean;

import commonInfrastructure.accountManagement.session.EmailSessionBean;
import frm.billingAndPaymentProcessing.entity.DunningEmailEntity;
import frm.billingAndPaymentProcessing.entity.InvoiceEntity;
import frm.billingAndPaymentProcessing.session.DunningEmailSessionBean;
import frm.billingAndPaymentProcessing.session.InvoiceSessionBean;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;

/**
 *
 * @author xhx
 */
@ManagedBean
@RequestScoped
public class CommenceDunningProcessManagedBean {

    private Long invoiceId;
    private InvoiceEntity invoice;
    private DunningEmailEntity dunningEmail;
    @EJB
    InvoiceSessionBean invoiceSessionBean;
    @EJB
    DunningEmailSessionBean dunningEmailSessionBean;
    @EJB
    EmailSessionBean emailSessionbean;

    /** Creates a new instance of CommenceDunningProcessManagedBean */
    public CommenceDunningProcessManagedBean() {

    }
    
    
    public void initView(PhaseEvent envet){
          if (invoice == null) {
            setInvoice((InvoiceEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("invoice"));
        }
          invoiceId = invoice.getId();
    }
    public void createDunningEmail() {
        invoice = invoiceSessionBean.getInvoiceById(invoiceId);
        if (checkInvoice(invoice)) {
            String emailAddress = invoice.getDeliveryOrder().getParentSalesOrder().getCustomer().getEmail();
            emailSessionbean.sendDunningEmail(emailAddress, invoice);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Dunning Email is Successfully sent to customer", ""));
        }
        else{
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Invoice Has been cleaned, Please check whether you choosed the correct Invoice ", ""));
        return;    
        }
    }

    private Boolean checkInvoice(InvoiceEntity invoiceEntity) {
        Boolean isReady = false;
        if (invoiceEntity.getPayment_balance() >= 0) {
            isReady = false;
        } else {
            isReady = true;
        }
        return isReady;
    }

    public InvoiceEntity getInvoice() {
        return invoice;
    }

    public DunningEmailEntity getDunningEmail() {
        return dunningEmail;
    }

    public void setDunningEmail(DunningEmailEntity dunningEmail) {
        this.dunningEmail = dunningEmail;
    }

    public Long getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(Long invoiceId) {
        this.invoiceId = invoiceId;
    }

    public void setInvoice(InvoiceEntity invoice) {
        this.invoice = invoice;
    }
}
